function [GG,RR,Cobs,eu,SDX,ZZ,initss,ssvec,flag,ssnames,namesstates,namesshocks,GAM0,GAM1,PSI,PPI] =...
    modBFM_modver01(param,~,addsol)
%{
    Inputs:
    \begin{itemize}
    \item |param| Parameter Vector
    \item |~| (Currently Unused)
    \item |addsol| Additional settings
    \end{itemize}
    
    Outputs:
    \begin{itemize}
    \item |GG| Coefficient Matrix on Lagged Endogenous Variables
    \item |RR| Coefficient Matrix on Exogenous Shocks
    \item |Cobs| Vector of Constants
    \item |eu| Existence and Uniqueness
    \item |SDX| Standard Deviations of Exogenous Shocks
    \item |ZZ| Coefficient Matrix on Observables
    \item |initss| (Currently Unused)
    \item |ssvec| Values of Important Steady-State Variables
    \item |flag| Important Flags
    \item |ssnames| Names of Steady-State Values
    \item |namesstates| Names of States
    \item |namesshocks| Names of Shocks
    \item |GAM0| Coefficient Matrix on Contemporaneous Endogenous
    Variables (Useful for Debugging)
    \item |GAM1| Coefficient Matrix on Lagged Endogenous Variables (Useful
    for Debugging)
    \item |PPI| Coefficient Matrix on Expectations Errors(Useful
    for Debugging)
    \item |PSI| Coefficient Matrix on Exogenous Variables (Useful for
    Debugging)
    \end{itemize}
    %}
    
    initss  = [];     ssvec   = [];
    
    flag.ssok   = 1;    % Steady-State OK (Not Changed in this Model)
    flag.solver = 1;    % Which Solver to Use (Not Change in this model)
    
    % =========================================================================
    %%  PART I: Parameters and Positions
    
    AD          = param(1);     % average Duration of Government Debt
    bet         = param(2);     % discount factor
    delt        = param(3);     % private capital depreciation rate
    alph        = param(4);     % share of capital in the prod. function
    etaw        = param(5);     % elasticity of substitution b/w labor talents
    etap        = param(6);     % elasticity of substitution b/w intermediate goods
    sgc         = param(7);     % steady state government consumption to model Y ratio;
    sb          = param(8);     % steady state government debt to model Y ratio
    taul        = param(9);     % steady state labor tax rate
    tauk        = param(10);    % steady state capital tax rate
    tauc        = param(11);    % steady state consumption tax rate
    gamm100     = param(12);    % steady state growth rate of technology, multiplied by 100 (quarterly)
    pistar100   = param(13);    % steady state inflation rate multiplied by 100 (quarterly)
    xi          = param(14);    % inverse Frisch elasticity
    muHH        = param(15);    % fraction of non-savers in population
    omegaw      = param(16);    % prob. of unions not resetting price
    omegap      = param(17);    % prob. of firms not resetting price
    gpsi        = param(18);    % K utilization cost parameter
    s           = param(19);    % s" in the investment adjustment cost function
    chiw        = param(20);    % percentage of unions' wage adjustment when they cannot optimize
    chip        = param(21);    % percentage of intermediate firms' price adjustment when they cannot optimize
    thet        = param(22);    % habit formation
    alphag      = param(23);    % substitutability (>0) or complementarity (< 0) of private and public consumption
    
    phipi       = param(24);    % Taylor rule inflation coefficient
    phiy        = param(25);    % Taylor rule output coefficient
    phipic      = param(26);    % Taylor rule inflation coefficient COUNTERFACTUAL ECONOMY
    gammgc      = param(27);    % response of government consumption to debt
    gammtk      = param(28);    % response of capital tax to debt
    gammtl      = param(29);    % response of labor tax to debt
    gammtc      = param(30);    % response of consumption tax to debt
    gammz       = param(31);    % response of saver transfers to debt
    phizy       = param(32);    % Transfers response to GDP (automatic stabilizers)
    gammgcc     = param(33);    % response of government consumption to debt COUNTERFACTUAL ECONOMY
    gammtkc     = param(34);    % response of capital tax to debt COUNTERFACTUAL ECONOMY
    gammtlc     = param(35);    % response of labor tax to debt COUNTERFACTUAL ECONOMY
    gammtcc     = param(36);    % response of consumtpion tax to debt COUNTERFACTUAL ECONOMY
    gammzc      = param(37);    % response of saver transfers to debt COUNTERFACTUAL ECONOMY
    nfrp        = param(38);    % if 0, it shuts down the unfunded transfers shocks defining a counterfactual economy, 1 no shock-specific rule (counterfactual economy hit by same shocks as the actual economy)
    rhor        = param(39);    % serial correlation on interest rate in Taylor rule
    rhogc       = param(40);    % serial correlation on gov. consumption rule
    rhoz        = param(41);    % serial correlation on savers transfers rule
    rhotk       = param(42);    % serial correlation on capital tax rule
    rhotl       = param(43);    % serial correlation on labor tax rule
    rhotc       = param(44);    % serial correlation on consumption tax rule
    rhoeg       = param(45);    % AR coefficients on government consumption policy shocks
    rhoeMz      = param(46);    % AR coefficients on funded transfers policy shocks
    rhoeFz      = param(47);    % AR coefficients on unfunded transfers policy shocks
    rhoa        = param(48);    % serial correlation on technological productivity growth
    rhob        = param(49);    % serial correlation on preference shock
    rhoem       = param(50);    % AR coefficients on monetary policy shocks
    rhoi        = param(51);    % serial correlation on investment preference shock
    rhow        = param(52);    % serial correlation on wage markup shock
    rhop        = param(53);    % serial correlation on price markup shock
    rhorp       = param(54);    % serial correlation risk premium shock
    rhoCP       = param(55);    % serial correlation pers. cost push
    rhozt       = param(56);    % serial correlation transitory transfer component (funded)
    
    ssR         = param(57);    % 1 Shock Specicic Rules in the actual economy, else traditional policy rules with counterfcatual economy becoming ininfluential for outcomes in the actual economy
    
    startParSTD=58;
    siggc       = param(startParSTD);  % gov. consumption shock standard deviation
    sigMz       = param(startParSTD+1);  % FUNDED transfer shock standard deviation ()
    sigFz       = param(startParSTD+2);  % UNFUNDED transfer shock standard deviation ()
    siga        = param(startParSTD+3);    % technology shock standard deviation
    sigb        = param(startParSTD+4);  % preference shock standard deviation
    sigm        = param(startParSTD+5);  % monetary policy shock standard deviation
    sigi        = param(startParSTD+6);  % investment preference shock standard deviation
    sigw        = param(startParSTD+7);  % wage markup shock standard deviation
    sigp        = param(startParSTD+8);  % price markup shock standard deviation
    sigrp       = param(startParSTD+9);  % risk premium shock
    sigCP       = param(startParSTD+10); % St.dev. persistent cost push shock
    sigzt       = param(startParSTD+11); % St.dev.  transitory transfer component (funded)
    STD_gdpME   = param(startParSTD+12); %stdev measurement error for GDP growth
    STD_byME    = param(startParSTD+13); %stdev measurement error for debt-to-gdp ratio
    STD_beiME   = param(startParSTD+14); %stdev measurement error for 5-yr Breakeven inflation expectations
    flagEFFR    = param(startParSTD+15); % 1 if expected FFR observed (second sample only)
    numSignal   = param(startParSTD+16); % Number of forward guidance signals
    flagEINFL   = param(startParSTD+17); %1 if inflation expectations (BEI) are observed
    InflExpHor  = param(startParSTD+18); % Horizon of inflation expectations (in quarters)
    
    %Forward guidance shocks parameters (second sample)
    startFG     = startParSTD+18;
    FG_loc      = linspace(1+startFG,1+startFG+(1+numSignal)*3,4);
    Aload       = param(FG_loc(1):FG_loc(2)-1);
    Bload       = param(FG_loc(2):FG_loc(3)-1);
    STD         = param(FG_loc(3):FG_loc(4)-1);
    FactA_STD   = param(max(FG_loc));
    FactB_STD   = param(max(FG_loc)+1);
    
    %%  PART II: Position of Variables
    
    csLog=1;         % consumption savers
    cnLog=2;         % consumption: Non-Savers
    RLog=3;          % nominal interest rate
    iLog=4;          % investment
    kLog=5;          % effective capital
    vLog=6;          % capital utilization rate
    lLog=7;          % labor
    yLog=8;          % output
    gcLog=9;         % govt consumption
    cLog=10;         % aggregate consumption
    qLog=11;         % Lagrangian multiplier for a unit of investment good
    rkLog=12;        % real return for private k
    wLog=13;         % real wage
    piLog=14;        % inflation
    bLog=15;         % govt debt
    sbLog=16;        % b/y ratio
    taukLog=17;      % tauk
    taulLog=18;      % taul
    taucLog=19;      % tauc
    rLog=20;         % real interest rate
    zLog=21;         % transfers total (\hat{z}_t=\hat{z}^b_t+\zeta_t^M+\zeta_t^F)
    zendLog=22;      % transfers: responses to debt and business cycles and transitory movements - all funded transfers  (\hat{z}^b_t -eq (19))
    mcLog=23;        % real marginal cost
    kbarLog=24;      % private capital
    lambdaLog=25;    % household Lagrangian multiplier from budget constraint
    PbLog=26;        % price of bonds
    cstarLog=27;     % consumption in utility function
    piLLog=28;       % long-run inflation
    rLLog=29;        % long-run interest rate
    SLog=30;         % primary surplus
    rbLog=31;        % rb defined
    TkLog=32;        % capital tax revenue
    TlLog=33;        % labor tax revenue
    TcLog=34;        % consumption tax revenue
    % COUNTERFACTUAL ECONOMY
    csLogc=35;         % consumption savers
    cnLogc=36;         % consumption: Non-Savers
    RLogc=37;          % nominal interest rate
    iLogc=38;          % investment
    kLogc=39;          % effective capital
    vLogc=40;          % capital utilization rate
    lLogc=41;          % labor
    yLogc=42;          % output
    gcLogc=43;         % govt consumption
    cLogc=44;         % aggregate consumption
    qLogc=45;         % Lagrangian multiplier for a unit of investment good
    rkLogc=46;        % real return for private k
    wLogc=47;         % real wage
    piLogc=48;        % inflation
    bLogc=49;         % govt debt
    sbLogc=50;        % b/y ratio
    taukLogc=51;      % tauk
    taulLogc=52;      % taul
    taucLogc=53;      % tauc
    rLogc=54;         % real interest rate
    zLogc=55;         % transfers total
    zendLogc=56;      % transfers transitory component plus responses to business cycles and debt
    mcLogc=57;        % real marginal cost
    kbarLogc=58;      % private capital
    lambdaLogc=59;    % household Lagrangian multiplier from budget constraint
    PbLogc=60;        % price of bonds
    cstarLogc=61;     % consumption in utility function
    piLLogc=62;       % long-run inflation
    rLLogc=63;        % long-run interest rate
    SLogc=64;         % primary surplus
    rbLogc=65;        % rb defined
    TkLogc=66;        % capital tax revenue
    TlLogc=67;        % labor tax revenue
    TcLogc=68;        % consumption tax revenue
    
    % Observable Variables (used for filtering)
    yobs=69;
    cobs=70;
    iobs=71;
    gobs=72;
    wobs=73;
    zobs=74;
   
    % AR exogenous processes
    startShocks = 75;
    shkgcLog     = startShocks;      %1  government consumption shock
    shkzMLog      = startShocks+1;    %2  FUNDED transfers shock
    shkzFLog    = startShocks+2;    %3  UNFUNDED transfers shock
    shkaLog      = startShocks+3;    %4  technology shock
    shkbLog      = startShocks+4;    %5  preference shock
    shkmLog      = startShocks+5;    %6  monetary policy shock
    shkiLog      = startShocks+6;    %7  investment specific technology shock
    shkwLog      = startShocks+7;    %8  wage markup shock
    shkpLog      = startShocks+8;    %9  price markup shock
    shkrpLog     = startShocks+9;    %10  risk premium shock
    shkCPLog = startShocks+10;    %11  Persistent cost-push shock
    shkztempLog    = startShocks+11;   %12 Transitory shock to transfers (funded)
    currentplace    = startShocks+11;
    
    
    % Forward-guidance shocks (only second sample) - Factor model
    signalOrder={'one','two','three','four','five','six','seven','eight',...
        'nine','ten','eleven','twelve',...
        'thirteen','fourteen','fifteen','sixtheen','seventeen','eighteen','nineteen','twenty'};
    posNews=[];
    
    for signum = 1:numSignal
        strField = signalOrder{signum};
        tempPos = currentplace+1:currentplace+1+signum-1;
        posNews.(strField).pos = tempPos;
        posNews.(strField).N   = signum;
        currentplace = max(tempPos);
        posNews.SigLoc(signum) = max(tempPos);
    end
    
    shkfactorA  = currentplace+1; 
    shkfactorB  = currentplace+2; 
    
    %% Expectation Variables
    %%
    jj=0; ii=currentplace+2;
    EpiLog=ii+1;ii=ii+1;jj=jj+1;
    ErLLog=ii+1;ii=ii+1;jj=jj+1;
    EPbLog=ii+1;ii=ii+1;jj=jj+1;
    ElambdaLog=ii+1;ii=ii+1;jj=jj+1;
    ErkLog=ii+1;ii=ii+1;jj=jj+1;
    EtaukLog=ii+1;ii=ii+1;jj=jj+1;
    EqLog=ii+1;ii=ii+1;jj=jj+1;
    EiLog=ii+1;ii=ii+1;jj=jj+1;
    EwLog=ii+1;ii=ii+1;jj=jj+1;
    
    EpiLogc=ii+1;ii=ii+1;jj=jj+1;
    ErLLogc=ii+1;ii=ii+1;jj=jj+1;
    EPbLogc=ii+1;ii=ii+1;jj=jj+1;
    ElambdaLogc=ii+1;ii=ii+1;jj=jj+1;
    ErkLogc=ii+1;ii=ii+1;jj=jj+1;
    EtaukLogc=ii+1;ii=ii+1;jj=jj+1;
    EqLogc=ii+1;ii=ii+1;jj=jj+1;
    EiLogc=ii+1;ii=ii+1;jj=jj+1;
    EwLogc=ii+1;ii=ii+1;jj=jj+1;
    
    nend=jj;
    
    % Final Counts
    NVarY   =ii; %Lobs;
    Neq     =ii; %Lobs;
    
    %%  PART III: Position of shocks
    
    egcLog     = 1;                 %1  government consumption shock
    ezMLog      = 2;                 %2  funded transfers shock
    ezFLog    = 3;                 %3  unfunded transfers shock
    eaLog      = 4;                 %4  technology shock
    ebLog      = 5;                 %5  preference shock
    emLog      = 6;                 %6  monetary policy shock
    eiLog      = 7;                 %7  investment specific technology shock
    ewLog      = 8;                 %8  wage markup shock
    epLog      = 9;                 %9  price markup shock
    erpLog     = 10;                %10  risk-premium shock
    eCPLog = 11;                    %11  persistent cost-push shocks
    eztLog    =12;                  %12 transitory transfer shocks
    factorA    =13;                 %13 FWG target factor
    factorB    =14;                 %14 FWG path factor
    currentplace=15;
    
    ER_Signal = currentplace:currentplace+numSignal-1; 
    if isempty(ER_Signal)
        NShocks=currentplace-1;
    else
        NShocks=ER_Signal(end);
    end
    
    % Matrix of Standard Deviations
    SDX=eye(NShocks);
    
    SDX(egcLog,egcLog)              = siggc;
    SDX(ezMLog,ezMLog)                = sigMz;
    SDX(ezFLog,ezFLog)            = sigFz;
    SDX(eaLog,eaLog)                = siga;
    SDX(ebLog,ebLog)                = sigb;
    SDX(emLog,emLog)                = sigm;
    SDX(eiLog,eiLog)                = sigi;
    SDX(ewLog,ewLog)                = sigw;
    SDX(epLog,epLog)                = sigp;
    SDX(erpLog,erpLog)              = sigrp;
    SDX(eCPLog,eCPLog)             = sigCP;
    SDX(eztLog,eztLog)            = sigzt;
    SDX(factorA,factorA)            = FactA_STD;
    SDX(factorB,factorB)            = FactB_STD;
    
    SDX(ER_Signal,ER_Signal)    = diag(STD(2:end)); 
    
    %%  PART IV: Model Declaration
    GAM0   = zeros(Neq,NVarY);
    GAM1    = zeros(Neq,NVarY);
    PSI     = zeros(Neq,NShocks);
    PPI     = zeros(Neq,nend);
    CC      = zeros(Neq,1);
    %%  PART V: Steady-State
    Rho = (1 - (1/AD))*(1/bet);
    gamm = gamm100/100;
    expg = exp(gamm);
    Rss = expg/bet*exp(pistar100/100);
    Rbar = (Rss - 1)*100;
    Pb = 1/(Rss - Rho);  
    Rk = (exp(gamm)/bet - 1 + delt)/(1-tauk);
    psi1 = Rk*(1-tauk);
    mc = 1/(1 + etap);
    w = (mc*((1-alph)^(1-alph))*(alph^alph)*(Rk^(-alph)))^(1/(1-alph));
    KL = (w/Rk)*alph/(1-alph);
    OmegL = (KL^alph) - Rk*(KL) - w;
    YL = (KL^alph) - OmegL;
    IL = (1-(1-delt)*exp(-gamm))*expg*KL;
    CL = YL*(1 - sgc) - IL;
    rk = Rk;
    ZL = ((1 - Rss*exp(-gamm))*sb - sgc)*YL + (tauc)*CL + taul*w + tauk*rk*KL;
    ZnL = ZL;
    CnL = ((1-taul)*w + ZnL)/(1+tauc);
    CsL = (CL - muHH*CnL)/(1-muHH);
    CstarL = CsL + alphag*sgc*YL;
    l = ((w*(1-taul)/((1+tauc)*(1+etaw)))*(1/((1-thet*exp(-gamm))*CstarL)))^(1/(xi + 1));
    cs = CsL*l;
    cn = CnL*l;
    y = YL*l;
    k = KL*l;
    Omeg = OmegL*l;
    c = CL*l;
    inv = IL*l;
    z = ZL*l;
    b = sb*y;
    gc = sgc*y;
    ky = k/y;
    cy = c/y;
    ly = l/y;
    TK = tauk*rk*k;
    TL = taul*l*w;
    TC = tauc*c;
    S = tauk*rk*k + taul*l*w + tauc*c - gc - z;
    
    
    %%  PART VI: Equations actual economy
    % 1) Production Function 
    GAM0(1,yLog)  = 1;
    GAM0(1,kLog)  = -((y+Omeg)/y)*alph;
    GAM0(1,lLog)  = -((y+Omeg)/y)*(1-alph);
    
    % 2) Production Factors   
    GAM0(2,rkLog)  = 1;
    GAM0(2,wLog)   =-1;
    GAM0(2,kLog)   = 1;
    GAM0(2,lLog)   =-1;
    
    % 3) Marginal Cost 
    
    GAM0(3,mcLog)  =1;
    GAM0(3,rkLog)  =-alph;
    GAM0(3,wLog)   =alph-1;
    
    % 4) Price Phillips Curve
    lamprice = ((1+bet*chip)*omegap)/((1-bet*omegap)*(1-omegap)); %this is the inverse slope
    GAM0(4,piLog)       = lamprice;
    GAM0(4,mcLog)       = -1;
    GAM0(4,shkpLog)     =-lamprice;
    
    GAM1(4,piLog)        = lamprice*chip/(1+bet*chip);
    
    GAM0(4,EpiLog)       = -lamprice*bet/(1+bet*chip);
    GAM0(4,shkCPLog)  =-1;
    
    % 5) Household FOC for Consumption (Savers' Lagrange Multiplier) 
    GAM0(5,lambdaLog) = 1;
    GAM0(5,cstarLog)  = expg/(expg-thet);
    GAM0(5,taucLog)   = (tauc/(1+tauc));
    GAM0(5,shkbLog)   =-1;
    GAM0(5,shkaLog)   = thet/(expg-thet);
    
    GAM1(5,cstarLog)  = thet/(expg-thet);
    
    % 6) Long-Run Real Interest Rate 
    GAM0(6,rLLog) = 1;
    GAM0(6,PbLog) = 1;
    
    GAM0(6,ErLLog) = -bet*Rho/expg;
    GAM0(6,EPbLog) = -bet*Rho/expg;
    GAM0(6,EpiLog) = 1;
    
    
    % 7) Long-Run Inflation Rate 
    GAM0(7,piLLog)  = 1;
    GAM0(7,PbLog)   = 1;
    GAM0(7,rLLog)   = 1;
    
    
    % 8) Consumption in Utility 
    GAM0(8,cstarLog)  = 1;
    GAM0(8,csLog)     =-cs/(cs + alphag*gc);
    GAM0(8,gcLog)     =-alphag*gc/(cs + alphag*gc);
    
    
    % 9) Euler equation 
    GAM0(9,lambdaLog)      = 1;
    GAM0(9,RLog)           =-1;
    GAM0(9,shkrpLog)       =-1;  
    GAM0(9,shkaLog)        = rhoa;
    
    GAM0(9,EpiLog)          = 1;
    GAM0(9,ElambdaLog)      =-1;
    
    % 10) FOC Capacity Utilization 
    GAM0(10,rkLog) = ((1-gpsi)/gpsi);
    GAM0(10,vLog)  = -1;
    GAM0(10,taukLog) = -(((1-gpsi)/gpsi)*tauk/(1-tauk));
    
    % 11) FOC Capital
    GAM0(11,qLog)       =1;
    GAM0(11,RLog)       =1;
    GAM0(11,shkrpLog)   =1;  

    GAM0(11,ErkLog)      = -bet*exp(-gamm)*Rk*(1-tauk);
    GAM0(11,EtaukLog)    = tauk*exp(-gamm)*bet*Rk;
    GAM0(11,EqLog)       = -bet*exp(-gamm)*(1-delt);
    GAM0(11,EpiLog)      = -1;
    
    % 12) Investment FOC  
    GAM0(12,iLog)     = 1;
    GAM0(12,qLog)     =-1/((1+bet)*s*(exp(2*gamm)));
    GAM0(12,shkaLog)  = (1-bet*rhoa)/(1+bet);
    GAM0(12,shkiLog)  =-1;
    
    GAM1(12,iLog)      = 1/(1+bet);
    GAM0(12,EiLog)     =-bet/(1+bet);
    
    % 13) Effective Capital 
    GAM0(13,kLog)       = 1;
    GAM0(13,vLog)       =-1;
    GAM0(13,shkaLog)    = 1;
    
    GAM1(13,kbarLog)     = 1;
    
    % 14) Law of Motion for Capital 
    GAM0(14,kbarLog)    = 1;
    GAM0(14,shkaLog)    = (1-delt)*exp(-gamm);
    GAM0(14,shkiLog)    =-(1 - (1-delt)*exp(-gamm))*((1+bet)*s*(exp(2*gamm)));
    GAM0(14,iLog)       =-(1 - (1-delt)*exp(-gamm));
    
    GAM1(14,kbarLog)     = (1-delt)*exp(-gamm);
    
    % 15) Wage Equation 
    lamwage = (omegaw*(1+bet)*(1+xi*(1+(1/etaw))))/((1-omegaw*bet)*(1-omegaw));
    GAM0(15,wLog)      = 1 + lamwage;
    GAM0(15,lLog)      =-xi;
    GAM0(15,taulLog)   = -(taul/(1-taul));
    GAM0(15,lambdaLog) = 1;
    GAM0(15,piLog)     = (lamwage*(1 + bet*chiw)/(1 + bet));
    GAM0(15,shkbLog)   =-1;
    GAM0(15,shkaLog)   = lamwage*((1 + bet*chiw - rhoa*bet)/(1+bet));
    GAM0(15,shkwLog)   =-lamwage;
    
    GAM1(15,wLog)       =(lamwage/(1+bet));
    GAM1(15,piLog)      =(lamwage*chiw/(1 + bet));
    GAM1(15,shkaLog)    =(lamwage*chiw/(1+bet));
    
    GAM0(15,EwLog)      =-lamwage*(bet/(1+bet));
    GAM0(15,EpiLog)     =-(lamwage*bet/(1+bet));
    
    % 16) Monetary Policy Rule
    if ssR==0
        GAM0(16,RLog)          = 1;
        GAM0(16,piLog)         =-(1-rhor)*phipi;
        GAM0(16,yLog)          =-(1-rhor)*phiy;
        GAM0(16,shkmLog)       =-1;
        
        GAM1(16,RLog)           = rhor;
    else
        GAM0(16,RLog)          = 1;
        GAM0(16,piLog)         =-(1-rhor)*phipi;
        GAM0(16,piLogc)        =-(1-rhor)*(phipic-phipi);
        GAM0(16,yLog)          =-(1-rhor)*phiy;
        GAM0(16,yLogc)          =-(1-rhor)*(phiy-phiy);
        GAM0(16,shkmLog)       =-1;
        
        GAM1(16,RLog)           = rhor;
    end
    for i = 1:numSignal
        tempStruct  = posNews.(signalOrder{i});
        temp.pos    = tempStruct.pos;
        temp.N      = tempStruct.N;
        
        GAM0(temp.pos(1),temp.pos(1)) = 1;
        
        PSI(temp.pos(1),factorA)       = Aload(i+1); 
        PSI(temp.pos(1),factorB)       = Bload(i+1); 
        PSI(temp.pos(1),ER_Signal(i))= 1;
        
        GAM1(16,temp.pos(end)) = 1;
        
        if i==1
            continue
        end
        
        GAM0(temp.pos(2:temp.N),temp.pos(2:temp.N))    = eye(temp.N-1);
        GAM1 (temp.pos(2:temp.N),temp.pos(1:temp.N-1))  = eye(temp.N-1);
        
    end
    
    % 17) Aggregate Resource Constraint 
    GAM0(17,cLog)         = c;
    GAM0(17,iLog)         = inv;
    GAM0(17,gcLog)        = sgc*y;
    GAM0(17,vLog)         = psi1*k;
    GAM0(17,yLog)         = -y;
    
    % 18) Non-Savers Household's Budget 
    GAM0(18,cnLog)        =  cn*(1+tauc);
    GAM0(18,taucLog)      =  tauc*cn;
    GAM0(18,wLog)         = -w*l*(1-taul);
    GAM0(18,lLog)         = -w*l*(1-taul);
    GAM0(18,taulLog)      =  w*l*taul;
    GAM0(18,zLog)         =  -1;
    
    % 19) Consumption Aggregation  
    GAM0(19,cLog)         = c;
    GAM0(19,csLog)        =-(1-muHH)*cs;
    GAM0(19,cnLog)        =-muHH*cn ;
    
    
    % 20) Maturity Structure 
    GAM0(20,RLog)        = 1;
    GAM0(20,shkrpLog)    = 1;
    GAM0(20,PbLog)       = 1;
    
    GAM0(20,EPbLog)        =-Rho*Pb/(1+Rho*Pb);
    
    
    % 21) Government Budget Constraint
    GAM0(21,bLog)         = sb;
    GAM0(21,taukLog)      = tauk*rk*ky;
    GAM0(21,rkLog)        = tauk*rk*ky;
    GAM0(21,kLog)         = tauk*rk*ky;
    GAM0(21,taulLog)      = taul*w*ly;
    GAM0(21,wLog)         = taul*w*ly;
    GAM0(21,lLog)         = taul*w*ly;
    GAM0(21,taucLog)      = tauc*cy;
    GAM0(21,cLog)         = tauc*cy;
    GAM0(21,piLog)        = sb/bet;
    GAM0(21,shkaLog)      = sb/bet;
    GAM0(21,PbLog)        =-sb*Rho*exp(-gamm);
    GAM0(21,gcLog)        =-sgc;
    GAM0(21,zLog)         =-(1/y);
    
    GAM1(21,bLog)          = sb/bet;
    GAM1(21,PbLog)         =-sb/bet;
    
    
    % 22) gc Rule
    if ssR==0
        GAM0(22,gcLog)          = 1;
        GAM0(22,shkgcLog)        =-1;
        
        GAM1(22,gcLog)           = rhogc;
        GAM1(22,sbLog)           =-(1-rhogc)*gammgc;
    else
        GAM0(22,gcLog)          = 1;
        GAM0(22,shkgcLog)        =-1;
%         GAM0(22,shkztempLog)     =-1;
        
        GAM1(22,gcLog)           = rhogc;
        GAM1(22,sbLog)           =-(1-rhogc)*gammgc;
        GAM1(22,sbLogc)           =-(1-rhogc)*(gammgcc-gammgc);
    end
    
    % 23) Capital Tax Rate Rule
    if ssR==0
        GAM0(23,taukLog)     = 1;
        
        GAM1(23,taukLog)      = rhotk;
        GAM1(23,sbLog)        = (1-rhotk)*gammtk;
    else
        GAM0(23,taukLog)     = 1;
        
        GAM1(23,taukLog)      = rhotk;
        GAM1(23,sbLog)        = (1-rhotk)*gammtk;
        GAM1(23,sbLogc)        = (1-rhotk)*(gammtkc-gammtk);
    end
    
    % 24)  Labor Tax Rate Rule
    if ssR==0
        GAM0(24,taulLog)     = 1;
        
        GAM1(24,taulLog)      = rhotl;
        GAM1(24,sbLog)        = (1-rhotl)*gammtl;
    else
        GAM0(24,taulLog)     = 1;
        
        GAM1(24,taulLog)      = rhotl;
        GAM1(24,sbLog)        = (1-rhotl)*gammtl;
        
        GAM1(24,sbLogc)        = (1-rhotl)*(gammtlc-gammtl);
    end
    % 25) Consumption Tax Rate Rule
    if ssR==0
        GAM0(25,taucLog)     = 1;
        
        GAM1(25,taucLog)      = rhotc;
        GAM1(25,sbLog)        =(1-rhotc)*gammtc;
    else
        GAM0(25,taucLog)     = 1;
        
        GAM1(25,taucLog)      = rhotc;
        GAM1(25,sbLog)        =(1-rhotc)*gammtc;
        GAM1(25,sbLogc)       =(1-rhotc)*(gammtcc-gammtc);
    end
    
    
    
    % 26-27)  Z Rule (tot+part)
    if ssR==0
        GAM0(26,zendLog)        = 1;
        GAM1(26,zendLog)         = rhoz;
        GAM1(26,sbLog)        =-(1-rhoz)*gammz;
    else
        GAM0(26,zendLog)        = 1;
        GAM0(26,shkztempLog)     =-1;
         GAM0(26,yLog)        = (1-rhoz)*phizy;
         
        GAM1(26,zendLog)         = rhoz;
        GAM1(26,sbLog)        =-(1-rhoz)*gammz;
        GAM1(26,sbLogc)       =-(1-rhoz)*(gammzc-gammz);
    end
    
    GAM0(27,zLog)=1;
    GAM0(27,zendLog)=-1;
    GAM0(27,shkzMLog)     =-1;
    GAM0(27,shkzFLog)   =-1;
    
    % 28)	Fisher Equation
    GAM0(28,rLog)        = 1;
    GAM0(28,RLog)        =-1;
    
    GAM0(28,EpiLog)       = 1;
    
    % 29)	sb Defined
    GAM0(29,sbLog)       = 1;
    GAM0(29,yLog)        = 1;
    GAM0(29,bLog)        =-1;
    
    % 30)  Consumption Tax Revenue
    GAM0(30,TcLog)       = 1;
    GAM0(30,taucLog)     =-1;
    GAM0(30,cLog)        =-1;
    
    % 31)	Capital Tax Revenue
    GAM0(31,TkLog)       = 1;
    GAM0(31,taukLog)     =-1;
    GAM0(31,rkLog)       =-1;
    GAM0(31,kLog)        =-1;
    
    % 32)	rb defined CHECK
    GAM0(32,rbLog)       = 1;
    GAM0(32,PbLog)       =-Rho*bet/expg;
    GAM0(32,piLog)       = 1;
    
    GAM1(32,PbLog)        =-1;
    
    %(33)	S defined CHECK
    GAM0(33,SLog)       = 1;
    GAM0(33,taukLog)    =-tauk*rk*k;
    GAM0(33,rkLog)      =-tauk*rk*k;
    GAM0(33,kLog)       =-tauk*rk*k;
    GAM0(33,taulLog)    =-taul*w*l;
    GAM0(33,wLog)       =-taul*w*l;
    GAM0(33,lLog)       =-taul*w*l;
    GAM0(33,taucLog)    =-tauc*c;
    GAM0(33,cLog)       =-tauc*c;
    GAM0(33,zLog)       = 1;
    GAM0(33,gcLog)      = gc;
    
    %  34)	Labor Tax Revenue
    GAM0(34,TlLog)       = 1;
    GAM0(34,taulLog)     =-1;
    GAM0(34,wLog)        =-1;
    GAM0(34,lLog)        =-1;
    
    %% Counterfactual economy
    
    % 35) Production Function
    GAM0(35,yLogc)  = 1;
    GAM0(35,kLogc)  = -((y+Omeg)/y)*alph;
    GAM0(35,lLogc)  = -((y+Omeg)/y)*(1-alph);
    
    % 36) Production Factors
    GAM0(36,rkLogc)  = 1;
    GAM0(36,wLogc)   =-1;
    GAM0(36,kLogc)   = 1;
    GAM0(36,lLogc)   =-1;
   
    % 37) Marginal Cost  
    
    GAM0(37,mcLogc)  =1;
    GAM0(37,rkLogc)  =-alph;
    GAM0(37,wLogc)   =alph-1;
    
    % 38) Price Phillips Curve
    lamprice = ((1+bet*chip)*omegap)/((1-bet*omegap)*(1-omegap)); %this is inverse of slope
    GAM0(38,piLogc)       = lamprice;
    GAM0(38,mcLogc)       = -1;
    GAM0(38,shkpLog)     =-lamprice;
   
    GAM1(38,piLogc)        = lamprice*chip/(1+bet*chip);
    GAM0(38,EpiLogc)       = -lamprice*bet/(1+bet*chip);
    GAM0(38,shkCPLog)  =-1;
    
    % 39) Household FOC for Consumption (Savers' Lagrange Multiplier) 
    GAM0(39,lambdaLogc) = 1;
    GAM0(39,cstarLogc)  = expg/(expg-thet);
    GAM0(39,taucLogc)   = (tauc/(1+tauc));
    GAM0(39,shkbLog)   =-1;
    GAM0(39,shkaLog)   = thet/(expg-thet);
    
    GAM1(39,cstarLogc)  = thet/(expg-thet);
    
    % 40) Long-Run Real Interest Rate 
    GAM0(40,rLLogc) = 1;
    GAM0(40,PbLogc) = 1;
    
    GAM0(40,ErLLogc) = -bet*Rho/expg;
    GAM0(40,EPbLogc) = -bet*Rho/expg;
    GAM0(40,EpiLogc) = 1;
    
    
    % 41) Long-Run Inflation Rate 
    GAM0(41,piLLogc)  = 1;
    GAM0(41,PbLogc)   = 1;
    GAM0(41,rLLogc)   = 1;
    
    
    % 42) Consumption in Utility 
    
    GAM0(42,cstarLogc)  = 1;
    GAM0(42,csLogc)     =-cs/(cs + alphag*gc);
    GAM0(42,gcLogc)     =-alphag*gc/(cs + alphag*gc);
    
    
    % 43) Euler equation 
    GAM0(43,lambdaLogc)      = 1;
    GAM0(43,RLogc)           =-1;
    GAM0(43,shkrpLog)        =-1;  
    GAM0(43,shkaLog)        = rhoa;
    
    GAM0(43,EpiLogc)          = 1;
    GAM0(43,ElambdaLogc)      =-1;
    
    % 44) FOC Capacity Utilization 
    GAM0(44,rkLogc) = ((1-gpsi)/gpsi);
    GAM0(44,vLogc)  = -1;
    GAM0(44,taukLogc) = -(((1-gpsi)/gpsi)*tauk/(1-tauk));
   
    % 45) FOC Capital 
    GAM0(45,qLogc)       =1;
    GAM0(45,RLogc)       =1;
    GAM0(45,shkrpLog)    =1;  
    
    GAM0(45,ErkLogc)      = -bet*exp(-gamm)*Rk*(1-tauk);
    GAM0(45,EtaukLogc)    = tauk*exp(-gamm)*bet*Rk;
    GAM0(45,EqLogc)       = -bet*exp(-gamm)*(1-delt);
    GAM0(45,EpiLogc)      = -1;
    
    % 46) Investment FOC  
    GAM0(46,iLogc)     = 1;
    GAM0(46,qLogc)     =-1/((1+bet)*s*(exp(2*gamm)));
    GAM0(46,shkaLog)  = (1-bet*rhoa)/(1+bet);
    GAM0(46,shkiLog)  =-1;
    
    GAM1(46,iLogc)      = 1/(1+bet);
    GAM0(46,EiLogc)     =-bet/(1+bet);
    
    % 47) Effective Capital 
    GAM0(47,kLogc)       = 1;
    GAM0(47,vLogc)       =-1;
    GAM0(47,shkaLog)    = 1;
    
    GAM1(47,kbarLogc)     = 1;
    
    % 48) Law of Motion for Capital 
    GAM0(48,kbarLogc)    = 1;
    GAM0(48,shkaLog)    = (1-delt)*exp(-gamm);
    GAM0(48,shkiLog)    =-(1 - (1-delt)*exp(-gamm))*((1+bet)*s*(exp(2*gamm)));
    GAM0(48,iLogc)       =-(1 - (1-delt)*exp(-gamm));
    
    GAM1(48,kbarLogc)     = (1-delt)*exp(-gamm);
    
    % 49) Wage Equation 
    lamwage = (omegaw*(1+bet)*(1+xi*(1+(1/etaw))))/((1-omegaw*bet)*(1-omegaw));
    GAM0(49,wLogc)      = 1 + lamwage;
    GAM0(49,lLogc)      =-xi;
    GAM0(49,taulLogc)   = -(taul/(1-taul));
    GAM0(49,lambdaLogc) = 1;
    GAM0(49,piLogc)     = (lamwage*(1 + bet*chiw)/(1 + bet));
    GAM0(49,shkbLog)   =-1;
    GAM0(49,shkaLog)   = lamwage*((1 + bet*chiw - rhoa*bet)/(1+bet));
    GAM0(49,shkwLog)   =-lamwage;
    
    GAM1(49,wLogc)       =(lamwage/(1+bet));
    GAM1(49,piLogc)      =(lamwage*chiw/(1 + bet));
    GAM1(49,shkaLog)    =(lamwage*chiw/(1+bet));
    
    GAM0(49,EwLogc)      =-lamwage*(bet/(1+bet));
    GAM0(49,EpiLogc)     =-(lamwage*bet/(1+bet));
    
    % 50) Monetary Policy Rule
    GAM0(50,RLogc)          = 1;
    GAM0(50,piLogc)         =-(1-rhor)*phipic;
    GAM0(50,yLogc)          =-(1-rhor)*phiy;
    GAM0(50,shkmLog)       =-1;
    
    GAM1(50,RLogc)           = rhor;
    
    % 51) Aggregate Resource Constraint 
    GAM0(51,cLogc)         = c;
    GAM0(51,iLogc)         = inv;
    GAM0(51,gcLogc)        = sgc*y;
    GAM0(51,vLogc)         = psi1*k;
    GAM0(51,yLogc)         = -y;
    
    % 52) Non-Savers Household's Budget 
    GAM0(52,cnLogc)        =  cn*(1+tauc);
    GAM0(52,taucLogc)      =  tauc*cn;
    GAM0(52,wLogc)         = -w*l*(1-taul);
    GAM0(52,lLogc)         = -w*l*(1-taul);
    GAM0(52,taulLogc)      =  w*l*taul;
    GAM0(52,zLogc)         =  -1;
    
    % 53) Consumption Aggregation  
    GAM0(53,cLogc)         = c;
    GAM0(53,csLogc)        =-(1-muHH)*cs;
    GAM0(53,cnLogc)        =-muHH*cn ;
    
    % 54) Maturity Structure 
    GAM0(54,RLogc)        = 1;
    GAM0(54,shkrpLog)    = 1;
    GAM0(54,PbLogc)       = 1;
    
    GAM0(54,EPbLogc)        =-Rho*Pb/(1+Rho*Pb);
    
    % 55) Government Budget Constraint
    GAM0(55,bLogc)         = sb;
    GAM0(55,taukLogc)      = tauk*rk*ky;
    GAM0(55,rkLogc)        = tauk*rk*ky;
    GAM0(55,kLogc)         = tauk*rk*ky;
    GAM0(55,taulLogc)      = taul*w*ly;
    GAM0(55,wLogc)         = taul*w*ly;
    GAM0(55,lLogc)         = taul*w*ly;
    GAM0(55,taucLogc)      = tauc*cy;
    GAM0(55,cLogc)         = tauc*cy;
    GAM0(55,piLogc)        = sb/bet;
    GAM0(55,shkaLog)       = sb/bet;
    GAM0(55,PbLogc)        =-sb*Rho*exp(-gamm);
    GAM0(55,gcLogc)        =-sgc;
    GAM0(55,zLogc)         =-(1/y);
    
    GAM1(55,bLogc)          = sb/bet;
    GAM1(55,PbLogc)         =-sb/bet;
    
    % 56) gc Rule
    GAM0(56,gcLogc)          = 1;
    GAM0(56,shkgcLog)       =-1;
    
    GAM1(56,gcLogc)           = rhogc;
    GAM1(56,sbLogc)           =-(1-rhogc)*gammgcc;
    
    % 57) Capital Tax Rate Rule PARAMETERS CHANGE IN COUNTERFACTUAL ECONOMY
    GAM0(57,taukLogc)     = 1;
    
    GAM1(57,taukLogc)      = rhotk;
    GAM1(57,sbLogc)        = (1-rhotk)*gammtkc;
    
    % 58)  Labor Tax Rate Rule PARAMETERS CHANGE IN COUNTERFACTUAL ECONOMY
    GAM0(58,taulLogc)     = 1;
    
    GAM1(58,taulLogc)      = rhotl;
    GAM1(58,sbLogc)        = (1-rhotl)*gammtlc;
    
    % 59) Consumption Tax Rate Rule PARAMETERS CHANGE IN COUNTERFACTUAL ECONOMY
    GAM0(59,taucLogc)     = 1;
    GAM1(59,taucLogc)     = rhotc;
    
    GAM1(59,sbLogc)       = (1-rhotc)*gammtcc;
    
     % 60-61)  Z Rule PARAMETERS CHANGE IN COUNTERFACTUAL ECONOMY
    GAM0(60,zendLogc)        = 1;
    GAM0(60,yLogc)          =(1-rhoz)*phizy;
    GAM0(60,shkztempLog)     =-1;
    
    GAM1(60,zendLogc)         = rhoz;
    GAM1(60,sbLogc)        =-(1-rhoz)*gammzc;
    
    GAM0(61,zLogc)          = 1;
    GAM0(61,zendLogc)       =-1;
    GAM0(61,shkzMLog)        =-1;
    GAM0(61,shkzFLog)      =-nfrp;
    
    % 62)	Fisher Equation
    GAM0(62,rLogc)        = 1;
    GAM0(62,RLogc)        =-1;
    
    GAM0(62,EpiLogc)       = 1;
    
    % 63)	sb Defined
    GAM0(63,sbLogc)       = 1;
    GAM0(63,yLogc)        = 1;
    GAM0(63,bLogc)        =-1;
    
    % 64)  Consumption Tax Revenue
    GAM0(64,TcLogc)       = 1;
    GAM0(64,taucLogc)     =-1;
    GAM0(64,cLogc)        =-1;
    
    % 65)	Capital Tax Revenue
    GAM0(65,TkLogc)       = 1;
    GAM0(65,taukLogc)     =-1;
    GAM0(65,rkLogc)       =-1;
    GAM0(65,kLogc)        =-1;
    
    % 66)	rb defined CHECK
    GAM0(66,rbLogc)       = 1;
    GAM0(66,PbLogc)       =-Rho*bet/expg;
    GAM0(66,piLogc)       = 1;
    
    GAM1(66,PbLogc)        =-1;
    
    % 67)	S defined CHECK
    GAM0(67,SLogc)       = 1;
    GAM0(67,taukLogc)    =-tauk*rk*k;
    GAM0(67,rkLogc)      =-tauk*rk*k;
    GAM0(67,kLogc)       =-tauk*rk*k;
    GAM0(67,taulLogc)    =-taul*w*l;
    GAM0(67,wLogc)       =-taul*w*l;
    GAM0(67,lLogc)       =-taul*w*l;
    GAM0(67,taucLogc)    =-tauc*c;
    GAM0(67,cLogc)       =-tauc*c;
    GAM0(67,zLogc)       = 1;
    GAM0(67,gcLogc)      = gc;
    
    %  68)	Labor Tax Revenue
    GAM0(68,TlLogc)       = 1;
    GAM0(68,taulLogc)     =-1;
    GAM0(68,wLogc)        =-1;
    GAM0(68,lLogc)        =-1;
    
    %% Exogenous Processes
    
    % 69) Government Spending
    GAM0(69,shkgcLog) = 1;
    PSI(69,egcLog)     = 1;
    GAM1(69,shkgcLog)  = rhoeg;
    
    % 70) Z Shock (funded)
    GAM0(70,shkzMLog)  = 1;
    PSI(70,ezMLog)      = 1;
    GAM1(70,shkzMLog)   = rhoeMz;

    % 71) Z Shock (unfunded)
    GAM0(71,shkzFLog)  = 1;
    PSI(71,ezFLog)      = 1;
    GAM1(71,shkzFLog)   = rhoeFz;
    
    % 72) Growth rate of Technology Shock
    GAM0(72,shkaLog)  = 1;
    PSI(72,eaLog)      = 1;
    GAM1(72,shkaLog)   = rhoa;
    
    % 73) Preference Shock
    GAM0(73,shkbLog)  = 1;
    PSI(73,ebLog)      = 1;
    GAM1(73,shkbLog)   = rhob;
    
    % 74) Monetary Policy Shock
    GAM0(74,shkmLog)  = 1;
    PSI(74,emLog)      = 1;
    GAM1(74,shkmLog)   = rhoem;
    
    % 75) Investment Shock
    GAM0(75,shkiLog)  = 1;
    PSI(75,eiLog)      = 1;
    GAM1(75,shkiLog)   = rhoi;
    
    % 76) Wage Markup Shock
    GAM0(76,shkwLog)  = 1;
    PSI(76,ewLog)      = 1;
    GAM1(76,shkwLog)   = rhow;
    
    % 77) Price Markup Shock
    GAM0(77,shkpLog)  = 1;
    PSI(77,epLog)      = 1;
    GAM1(77,shkpLog)   = rhop;
    
    % 78) Risk premium Shock
    GAM0(78,shkrpLog)  = 1;
    PSI(78,erpLog)      = 1;
    GAM1(78,shkrpLog)   = rhorp;
    
    % 79) Inflation drift Shocks *
    GAM0(79,shkCPLog)  = 1;
    PSI(79,eCPLog)      = 1;
    GAM1(79,shkCPLog)   = rhoCP;
    
    % 80) Transitory shocks to transfers (funded)*
    GAM0(80,shkztempLog)  = 1;
    PSI(80,eztLog)      = 1;
    GAM1(80,shkztempLog)   = rhozt;
   
    
    %% Observables
    
    % 81) Observable output growth
    GAM0(81,yobs)=1;
    GAM0(81,yLog)=-1;
    GAM0(81,shkaLog)=-1;
    
    GAM1(81,yLog)=-1;
    
    % 82) Observable consumption growth
    GAM0(82,cobs)=1;
    GAM0(82,cLog)=-1;
    GAM0(82,shkaLog)=-1;
    
    GAM1(82,cLog)=-1;
    
    %83) Observable investment growth
    GAM0(83,iobs)=1;
    GAM0(83,iLog)=-1;
    GAM0(83,shkaLog)=-1;
    
    GAM1(83,iLog)=-1;
    
    % 84) Observable real wage growth
    GAM0(84,wobs)=1;
    GAM0(84,wLog)=-1;
    GAM0(84,shkaLog)=-1;
    
    GAM1(84,wLog)=-1;
    
    % 85) Observable transfers growth
    GAM0(85,zobs)=1;
    GAM0(85,zLog)=-1;
    GAM0(85,shkaLog)=-1;
    
    GAM1(85,zLog)=-1;
    
    % 86) Government Spending Growth
    GAM0(86,gobs)=1;
    GAM0(86,gcLog)=-1;
    GAM0(86,shkaLog)=-1;
  
    GAM1(86,gcLog)=-1;
    
    %Expectations Errors
    if isempty(posNews)
        eq=86;
    else
        eq=max(posNews.SigLoc);
    end
    
    eq=eq+1;
    % IID Shocks: Policy Factors
    % 84) Factor A
    GAM0(eq,shkfactorA)=1;
    PSI(eq,factorA)=1;
    
    eq=eq+1;
    % 85) Factor B
    GAM0(eq,shkfactorB)=1;
    PSI(eq,factorB)=1;
    
    i=0;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,piLog)    = 1;
    GAM1(eq,EpiLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,piLogc)    = 1;
    GAM1(eq,EpiLogc)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,PbLog)    = 1;
    GAM1(eq,EPbLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,PbLogc)    = 1;
    GAM1(eq,EPbLogc)   = 1;
    PPI(eq,i) = 1;
    
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,rLLog)    = 1;
    GAM1(eq,ErLLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,rLLogc)    = 1;
    GAM1(eq,ErLLogc)   = 1;
    PPI(eq,i) = 1;
    
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,lambdaLog)    = 1;
    GAM1(eq,ElambdaLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,lambdaLogc)    = 1;
    GAM1(eq,ElambdaLogc)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,rkLog)    = 1;
    GAM1(eq,ErkLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,rkLogc)    = 1;
    GAM1(eq,ErkLogc)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,taukLog)    = 1;
    GAM1(eq,EtaukLog)   = 1;
    PPI(eq,i) = 1;
    
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,taukLogc)    = 1;
    GAM1(eq,EtaukLogc)   = 1;
    PPI(eq,i) = 1;
    
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,qLog)    = 1;
    GAM1(eq,EqLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,qLogc)    = 1;
    GAM1(eq,EqLogc)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,iLog)    = 1;
    GAM1(eq,EiLog)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,iLogc)    = 1;
    GAM1(eq,EiLogc)   = 1;
    PPI(eq,i) = 1;
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,wLog)    = 1;
    GAM1(eq,EwLog)   = 1;
    PPI(eq,i) = 1;
    
    
    eq=eq+1;
    i=i+1;
    GAM0(eq,wLogc)    = 1;
    GAM1(eq,EwLogc)   = 1;
    PPI(eq,i) = 1;
    
    %%  PART VII: Solve Model
    [GG,C,RR,fmat,fwt,ywt,gev,eu]=gensys(GAM0,GAM1,CC,PSI,PPI);
    flag.euok = isequal(eu(:),[1;1]);
    if isequal(eu(:),[1;1])==false
        
        % Clean
        ZZ  = [];
        Cobs= [];
        SDX = [];
        RR  = [];
        ssvec=[];
        ssnames={};
        namesstates={};
        namesshocks={};
        disp('Model has no solution')
        return
    end
    
    %% Remove Expectational Vars
    GG=GG(1:end-nend,1:end-nend); RR=RR(1:end-nend,:);
    
    %% ADD ME error for GDP growth
    [GG,RR,SDX,yobsme]=addMeasurementRow(yobs,1,STD_gdpME,GG,RR,SDX);
    
    %% ADD ME error for government bnd
    [GG,RR,SDX,sbobs]=addMeasurementRow(sbLog,1,STD_byME,GG,RR,SDX);
    
    %% ADD Expected Interest Rate
    %Expanding GG and RR to include Er
    aaa=zeros(numSignal,size(GG,2));
    for i=1:numSignal
        aa=GG^i; 
        aaa(i,:)=aa(RLog,:); 
    end
    GG1=aaa*GG; RR1=aaa*RR;
    GG=[[GG,zeros(size(GG,1),numSignal)];[GG1,zeros(size(GG1,1),numSignal)]];
    RR=[RR;RR1];
    EFFR=size(GG,2)-numSignal+1:size(GG,2);
    
    %% ADD Expected Inflation Rate
    %Expanding GG and RR to include Er
    
    aaa=zeros(InflExpHor,size(GG,2));
    for i=1:InflExpHor
        aa=GG^i; 
        aaa(i,:)=aa(piLog,:); 
    end
    GG2=aaa*GG; RR2=aaa*RR;
    GG=[[GG,zeros(size(GG,1),InflExpHor)];[GG2,zeros(size(GG2,1),InflExpHor)]];
    RR=[RR;RR2];
    if InflExpHor>0
        % Observed expectations (5-yr)
        GG3=ones(1,InflExpHor)*GG2/InflExpHor;
        RR3=ones(1,InflExpHor)*RR2/InflExpHor;
        GG=[[GG,zeros(size(GG,1),1)];[GG3,zeros(size(GG3,1),InflExpHor+1)]];
        RR=[RR;RR3];
        EINFL=size(GG,2);
    else
        EINFL=[];
    end
    
    %% ADD ME error for breakeven inflation
    [GG,RR,SDX,EINFLobs]=addMeasurementRow(EINFL,1,STD_beiME,GG,RR,SDX);
    
    
    %% Measurement Equations
    %inizialize Measurement Matrices
    vy=1;
    vc=2;
    vi=3;
    vh=4;
    vR=5;
    vwgr=6;
    vpi=7;
    vtr=8;
    vg=9;
    vb=10;
    if flagEFFR==0 && flagEINFL==0
        NObs=vb;
    elseif flagEFFR==1 && flagEINFL==0
        vEFFR=vb+1:vb+numSignal;
        NObs=vEFFR(end);
    elseif flagEFFR==0 && flagEINFL==1
        vEINFL=vb+1;
        NObs=vEINFL;
    elseif flagEFFR==1 && flagEINFL==1
        vEFFR=vb+1:vb+numSignal;
        vEINFL=vEFFR(end)+1;
        NObs=vEINFL;
    end
    
 
    ZZ=zeros(NObs,size(GG,1));
    Cobs=zeros(length(GG),1);
    
    %Output growth
    ZZ(vy,yobsme)=1;
    %Consumption growth
    ZZ(vc,cobs)=1;
    %Investment growth
    ZZ(vi,iobs)=1;
    % Hours
    ZZ(vh,lLog)=1;
    %Interest rate
    ZZ(vR,RLog)=1;
    %     %EFFR
    %     ZZ(vEFFR,EFFR(1:10))=eye(10);
    %Real wag growth
    ZZ(vwgr,wobs)=1;
    %Inflation
    ZZ(vpi,piLog)=1;
    %Transfer Growth Rate
    ZZ(vtr,zobs)=1;
    % Govt consumption
    ZZ(vg,gobs)=1;
    %Govt debt-to-GDP ratio
    ZZ(vb,sbobs)=1;
    if flagEFFR==1 && flagEINFL==0
        ZZ(vEFFR,EFFR)=eye(numSignal);
    elseif  flagEFFR==0 && flagEINFL==1
        ZZ(vEINFL(1),EINFLobs)=1;
    elseif  flagEFFR==1 && flagEINFL==1
        ZZ(vEFFR,EFFR)=eye(numSignal); 
        ZZ(vEINFL(1),EINFLobs)=1;
    end
    
    Cobs(yobsme)=gamm100;
    Cobs(cobs)=gamm100;
    Cobs(iobs)=gamm100;
    Cobs(gobs)=gamm100;
    Cobs(wobs)=gamm100;
    
    
    Cobs(sbobs)=log(sb)*100;
    Cobs(zobs)=gamm100;
    
    if  flagEFFR==0 && flagEINFL==0
        Cobs(RLog)=(log(Rss))*100; 
        Cobs(piLog)=pistar100;
    elseif flagEFFR==1 && flagEINFL==0
        Cobs([RLog,EFFR])=(log(Rss))*100;
        Cobs(piLog)=pistar100;
    elseif  flagEFFR==0 && flagEINFL==1
        Cobs(RLog)=(log(Rss))*100; 
        Cobs([piLog,EINFLobs])=pistar100;
    elseif  flagEFFR==1 && flagEINFL==1
        Cobs([RLog,EFFR])=(log(Rss))*100;
        Cobs([piLog,EINFLobs])=pistar100;
    end

    
    names.states  ={
        'consumption savers'
        'consumption: Non-Savers'
        'nominal interest rate'
        'investment'
        'effective capital'
        'capital utilization rate'
        'labor'
        'output'
        'govt consumption'
        'aggregate consumption'
        'Invest Lagrangian'
        'real return for private k'
        'real wage'
        'inflation'
        'govt debt'
        'b/y ratio'
        'tauk'
        'taul'
        'tauc'
        'real interest rate'
        'transfers total' 
        'transfers part'
        'real marginal cost'
        'private capital'
        'HH Lagrangian'
        'price of bonds'
        'consumption'
        'long-run inflation'
        'long-run interest rate'
        'primary surplus'
        'rb defined'
        'capital tax revenue'
        'labor tax revenue'
        'consumption tax revenue'
        % COUNTERFACTUAL ECONOMY
        'consumption savers CF'
        'consumption non-Savers CF'
        'nominal interest rate CF'
        'investment CF'
        'effective capital CF'
        'capital utilization rate CF'
        ' labor CF'
        'output CF'
        'govt consumption CF'
        'aggregate consumption CF'
        'Invest Lagrangian CF'
        'real return for private k CF'
        'real wage CF'
        'inflation CF'
        'govt debt CF'
        'b/y ratio CF'
        'tauk CF'
        'taulLog CF'
        'taucLog CF'
        'real interest rate CF'
        'transfers total CF'
        'transfers part CF'
        'real marginal cost CF'
        'private capital CF'
        'HH Lagrangian CF'
        'price of bonds CF'
        'consumption in utility CF'
        'long-run inflation CF'
        'long-run interest rate CF'
        'primary surplus CF'
        'rb defined CF'
        'capital tax revenue CF'
        'labor tax revenue CF'
        'consumption tax revenue CF'
        'yobs'
        'cobs'
        'iobs'
        'gobs'
        'wobs'
        'zobs'
        'g shock'
        'funded tranfer shock'
        'unfunded tranfer shock'
        'technology shock'
        'preference shock'
        'monetary policy shock'
        'investment specific technology shock'
        'wage markup shock'
        'price markup shock'
        'risk premium shock'
        'NKPC shock'
        'transitory trans shock'
        };
    for signum = 1:numSignal
    tempStruct  = posNews.(signalOrder{signum});
    tempN       = tempStruct.N;
    
    tempNames   = cellstr([repmat(['Signal ' num2str(signum) ', Lag: '],tempN,1) num2str((1:tempN)')]);
    names.states = cat(1,names.states,tempNames);
  end

Addition1 =     {        'shkfactorA'
        'shkfactorB'   };
 
names.states = cat(1,names.states,Addition1);
    Addition2 =     {'GDPobs_me'
        'Debt-To-GDP_me' };                
names.states = cat(1,names.states,Addition2);
names.states    = cat(1,names.states  , strcat(fillcell(numSignal,1,'EFFR '),num2str((1:numSignal)'))); 
names.states    = cat(1,names.states  , strcat(fillcell(InflExpHor,1,'EINFL '),num2str((1:InflExpHor)'))); 
if InflExpHor>0
    Addition3={'AvgInflExpectObs'    };
    names.states = cat(1,names.states,Addition3);
end
if InflExpHor>0
    Addition4={'BEIobs_me'    };
    names.states = cat(1,names.states,Addition4);
end
    names.shocks={
        'government consumption shock'
        'funded tranfers shock'
        'unfunded transfers shock'
        'technology shock'
        'preference shock'
        'monetary policy shock'
        'investment specific technology shock'
        'wage markup shock'
        'price markup shock'
        'risk-premium shock'
        'shock to NKPC'
        'transitory transfer shock'
        'FWG target factor'
        'FWG path factor'};
    
    names.shocks = cat(1,names.shocks, strcat(fillcell(numSignal,1,'FG Shock'),num2str(((1:numSignal)-1)')));  % 17:end-2
    
    Addition5={
        'ME GDP'
        'ME Debt-to-GDP Ratio'
        'ME BEI'
        };
names.shocks= cat(1,names.shocks,Addition5);
    
    
    
    ssvec   = [Rss gc c y inv TK TL TC z bet expg Rho rhogc b sb, taul, tauk,tauc, S,y,Rbar];
    ssnames = {'Rss', 'gc', 'c', 'y', 'inv', 'TK', 'TL', 'TC', 'z', 'bet', 'expg', 'Rho', 'rhogc', 'b', 'sb', 'taul', 'tauk','tauc', 'S','y','Rbar' };
    
    %%  PART XI: Extras
    %% add measurement errors ?????
    %%  PART XII: Output
    if size(names.states)~=size(GG,1)
        error('Wrong number of state namhes');
    end
    
    if size(names.shocks)~=size(RR,2)
        error('Wrong number of shock names');
    end
    
    namesstates         = names.states';
    namesshocks.short   = names.shocks';
    namesshocks.long    = names.shocks';
end
